//
//  BLActivityDetailViewController.m
//  家校互通
//
//  Created by 李赛 on 15/5/29.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//

#import "BLActivityDetailViewController.h"
#import "HWCommentTableViewCell.h"
#import "HWHeaderTableViewCell.h"
#import "HWInfoDetailTableViewCell.h"
#import "UITableView+FDTemplateLayoutCell.h"

#import "HcCustomKeyboard.h"//键盘操作
#import "ACDetailTableViewCell.h"
#import "ImageCollectionViewCell.h"
#import "BLCommentTableViewCell.h"
#import "BLCommentTableViewCell.h"
//#import "IMViewController.h"

#import "HWfooterViewReusableView.h"
#import "userHeaderImageCollectionViewCell.h"
#import "CommenBtnForImage.h"


#import <UIImageView+WebCache.h>
#import "HZPhotoBrowser.h"
@interface BLActivityDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,HZPhotoBrowserDelegate,UITextViewDelegate>
@property (nonatomic, assign) BOOL cellHeightCacheEnabled;
@property (nonatomic, retain) NSMutableArray *imageListArray;

@end

@implementation BLActivityDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"活动详情";
    _contentTableView.dataSource=self;
    _contentTableView.delegate=self;
    _contentTableView.estimatedRowHeight = 200;
    _contentTableView.fd_debugLogEnabled = YES;
//    _commentBtn.layer.masksToBounds=YES;
//    _commentBtn.layer.cornerRadius=10;
//    _commentBtn.layer.borderColor=[COLOR(220, 220, 220) CGColor];
//    _commentBtn.layer.borderWidth=0.5;
     _imageListArray=[NSMutableArray arrayWithArray:@[@"http://ww1.sinaimg.cn/thumbnail/67307b53jw1eoxmhonbmrg208c05thdv.gif",@"http://ww2.sinaimg.cn/thumbnail/98719e4agw1e5j49zmf21j20c80c8mxi.jpg",@"http://ww4.sinaimg.cn/thumbnail/996e0d92jw1eoxm33bf5hj20br0bqjso.jpg", @"http://ww2.sinaimg.cn/thumbnail/9b8146edjw1ep36zlbxphg205k07nnpg.gif",@"http://ww2.sinaimg.cn/thumbnail/642beb18gw1ep3629gfm0g206o050b2a.gif",@"http://ww4.sinaimg.cn/thumbnail/67307b53jw1ep3583mtd9j20p03hvkjl.jpg",@"http://ww4.sinaimg.cn/thumbnail/996e0d92jw1eoxm33bf5hj20br0bqjso.jpg",@"http://ww4.sinaimg.cn/thumbnail/996e0d92jw1eoxm33bf5hj20br0bqjso.jpg"]];
    self.cellHeightCacheEnabled = YES;
    [[HcCustomKeyboard customKeyboard] textViewShowView:self customKeyboardDelegate:self];
    [self createBottomHolderView];
    
    //已读
    [CoModel modifyRelation:^(BOOL result, NSError *error) {
        
    } withBlackboardId:_activityId withType:1 withStatus:1];
    // Do any additional setup after loading the view.
}
-(void)createBottomHolderView
{
    CommenBtnForImage *commentBtn=[[CommenBtnForImage alloc]initWithFrame:CGRectMake(0, 0, DSF_SCREENWIDTH/4, 35) withImageString:@"写评论" TitleName:@"写评论" withImageWidth:32];
    UITapGestureRecognizer *commentBtnTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(commentAction:)];
    [commentBtn addGestureRecognizer:commentBtnTap];
//    [commentBtn addTarget:self action:@selector(commentAction:) forControlEvents:UIControlEventTouchUpInside];
    [_holdView addSubview:commentBtn];
    
}

- (void)commentAction:(UIButton *)sender {
    [HcCustomKeyboard customKeyboard].placeHoldLable.text=@"我来说两句...";
    [[HcCustomKeyboard customKeyboard] talkAndReplyTextWithTypeTag:keyboardCommentTypeTalkAdd withReplyUserId:0 rootTalkId:0];
}


-(void)talkBtnClick:(UITextView *)textViewGet typeTag:(NSInteger)typeTag withReplyUserId:(NSInteger)replyUserId rootTalkId:(NSInteger)rootTalkId{
    [CoModel comment:^(BOOL result, NSError *error) {
        if (result) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"评论成功" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];
        }
    } withBlackboardId:_activityId withReceiverId:0 withContent:textViewGet.text];
}

- (IBAction)privateChatAction:(UIButton *)sender {
    UUChatViewController *vc =[self.storyboard instantiateViewControllerWithIdentifier:@"UUChatViewController"];

    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)shareBtnAction:(UIButton *)sender {
    
}
- (IBAction)collectionBtnAction:(UIButton *)sender {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    if (sender.selected) {
        sender.selected=NO;
        [CoModel modifyRelation:^(BOOL result, NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (result) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"取消收藏" message:nil delegate:sender cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alertView show];
                sender.selected = NO;
            }
        } withBlackboardId:_activityId withType:2 withStatus:0];
    }else{
        sender.selected=YES;
        [CoModel modifyRelation:^(BOOL result, NSError *error) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if (result) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"收藏成功" message:nil delegate:sender cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alertView show];
                sender.selected = YES;
            }
        } withBlackboardId:_activityId withType:2 withStatus:1];
    }

    
}


#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }else
        return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        ACDetailTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"ACDetailTableViewCell" forIndexPath:indexPath];
        [vCell setCollectionViewDataSourceDelegate:self contentIndex:collectionViewTagForContentMImage userImageIndex:collectionViewTagForUserHeaderImage];
        return vCell;
    }else
    {
        BLCommentTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"BLCommentTableViewCell" forIndexPath:indexPath];
        
        if(indexPath.row%2)
        {
            [vCell setTextWithcontentStr:@"CocoaChina是全球最大的苹果开发中文社区，官方微信每日定时推送各种精彩的研发教程资源和工具" withCommentName:@"小明（父亲）" replyName:nil];
        }else
        {
            [vCell setTextWithcontentStr:@"和我们平时说的字体不同，计算机意义上的字体表示的是同一大小，同一样式(Style)字形的集合。从这个意义上来说，当我们为文字设置粗体，斜体时其实是使用了另外一种字体(下划线不算)" withCommentName:@"小明（父亲）" replyName:@"王老师"];
        }
        return vCell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

#pragma mark -UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        ACDetailTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"ACDetailTableViewCell"];
        CGSize sizeToFit = [vCell.contentLB sizeThatFits:CGSizeMake(DSF_SCREENWIDTH-16, MAXFLOAT)];
        
        return 95+10+35+sizeToFit.height+80*3+5*2+10;
    
        
    }else
    {
        return [tableView fd_heightForCellWithIdentifier:@"BLCommentTableViewCell" configuration:^(BLCommentTableViewCell *vCell) {
            if(indexPath.row%2)
            {
                [vCell setTextWithcontentStr:@"CocoaChina是全球最大的苹果开发中文社区，官方微信每日定时推送各种精彩的研发教程资源和工具" withCommentName:@"小明（父亲）" replyName:nil];
            }else
            {
                [vCell setTextWithcontentStr:@"和我们平时说的字体不同，计算机意义上的字体表示的是同一大小，同一样式(Style)字形的集合。从这个意义上来说，当我们为文字设置粗体，斜体时其实是使用了另外一种字体(下划线不算)" withCommentName:@"小明（父亲）" replyName:@"王老师"];
            }
            //                [self configureCell:cell atIndexPath:indexPath];
        }];

    }
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    return nil;
}
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
//{
//
//}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}



#pragma mark---UICollectionViewDataSource--
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView.tag==collectionViewTagForContentMImage) {
        return _imageListArray.count;
    }else
        return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     if (collectionView.tag==collectionViewTagForContentMImage)
     {
         ImageCollectionViewCell *vCell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCollectionViewCell" forIndexPath:indexPath];
        [vCell.activityImage sd_setImageWithURL:[NSURL URLWithString:[_imageListArray objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"icon01.jpg"]];
         return vCell;
     }else
     {
         userHeaderImageCollectionViewCell *vCell=[collectionView dequeueReusableCellWithReuseIdentifier:@"userHeaderImageCollectionViewCell" forIndexPath:indexPath];
         return vCell;
     }
 
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag==collectionViewTagForUserHeaderImage) {
        HWfooterViewReusableView *footerView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"HWfooterViewReusableView" forIndexPath:indexPath];
        return footerView;
    }
    return nil;
}
#pragma mark---UICollectionViewDelegate--
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag==collectionViewTagForContentMImage)
    {
        
        HZPhotoBrowser *browserVc = [[HZPhotoBrowser alloc] init];
        browserVc.sourceImagesContainerView = collectionView; // 原图的父控件
        browserVc.imageCount =_imageListArray.count; // 图片总数
        browserVc.currentImageIndex = indexPath.row;
        browserVc.delegate = self;
        
        [self.view.window.rootViewController presentViewController:browserVc animated:NO completion:nil];
        
    }else
    {
        
    }
}
// 2.实现代理方法

// 返回临时占位图片（即原来的小图）
-(UIImage *)photoBrowser:(HZPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    UIImageView *imageView=[[UIImageView alloc]init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:[_imageListArray objectAtIndex:index]] placeholderImage:[UIImage imageNamed:@"icon01.jpg"]];;
    return imageView.image;
}

// 返回高质量图片的url
-(NSURL *)photoBrowser:(HZPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    NSString *urlStr = [[_imageListArray objectAtIndex:index] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
    return [NSURL URLWithString:urlStr];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
